/*
 * @Author: HaiHe_Team
 * @Date: 2023-02-01 11:49:20
 * @LastEditTime: 2023-02-01 14:12:12
 * @LastEditors: Joel
 * @Description: HaiHe Copyright 2022
 * @FilePath: \vscode_test_c_code\src\binary_tree.c
 */
#include"binary_tree.h"

//使用数组存放数据，注意是按照一层一层的排列，-1代表为空
int array[] = {1,2,3,4,5,6,7};
//构建二叉树
void createTree(Node **node,int index) {
	if (index <= (sizeof(array)/sizeof(int)) ){
		if (array[index-1] == -1) {
			*node = NULL;
			return;
		}
		else {
			*node = (Node*)malloc(sizeof(Node));
			(*node)->data = array[index-1]; 
			createTree(&((*node)->lchild), index * 2);
			createTree(&((*node)->rchild), index * 2 + 1);
		}
	}
	else {
		*node = NULL;
		return;
	}
}

void preOrder(Node* node) {
	if (node != NULL) {
		printf("%d ", node->data);
		preOrder(node->lchild);
		preOrder(node->rchild);
	}
	else {
		return;
	}
}

void midOrder(Node* node) {
	if (node != NULL) {
		midOrder(node->lchild);
		printf("%d ", node->data);
		midOrder(node->rchild);
	}
	else {
		return;
	}
}

void afterOrder(Node* node) {
	if (node != NULL) {
		afterOrder(node->lchild);
		afterOrder(node->rchild);
		printf("%d ", node->data);
	}
	else {
		return;
	}
}

int tree_array_test(void){
	Node* node_t;

	createTree(&node_t,1);

	preOrder(node_t);
	printf("\n");
	midOrder(node_t);
	printf("\n");
	afterOrder(node_t);


	return 0;
}
